window.onload = function () {
    //获取红，绿，黄灯以及倒计时的元素对象，字面量对象
    var lamp = {
        red: {//红灯
            obj: document.getElementById("red"),
            timeout: 10,
            style: ["red", "black", "black"],
            next: "green",
        },
        yellow: {//黄灯
            obj: document.getElementById("yellow"),
            timeout: 10,
            style: ["black", "yellow", "black"],
            next: "red",
        },
        green://绿灯
        {
            obj: document.getElementById("green"),
            timeout: 10,
            style: ["black", "black", "green"],
            next: "yellow",
        },
        changeStyle: function (style) {//倒计时元素对象
            this.red.obj.className = style[0];
            this.yellow.obj.className = style[1];
            this.green.obj.className = style[2];
        }
    };
    //计数对象
    var count = {
        obj: document.getElementById("count"),
        change: function (num) {//对于个位数，前面加0
            this.obj.innerHTML = num < 10 ? "0" + num : num;
        },
    };
    var now = lamp.green;//初始状态下绿灯
    var timeout = now.timeout;//初始状态下时间
    lamp.changeStyle(now.style);//初始状态下修改页面的样式
    count.change(timeout);//初始状态下的计数
    //设置定时器
    setInterval(function () {
        if (--timeout <= 0) {
            now = lamp[now.next];
            timeout = now.timeout;
            lamp.changeStyle(now.style);
        }
        count.change(timeout);
    }, 1000);
};